package com.joysuch.wwyt.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.core.common.util.SimpleDateFormatCache;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.log.bean.LogPageSearchBean;
import com.joysuch.wwyt.log.entity.FileUploadLog;
import com.joysuch.wwyt.log.mapper.FileUploadLogMapper;
import com.joysuch.wwyt.log.service.FileUploadLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * <p>
 * 日志
 * </p>
 *
 * @author zr
 * @since 2021-07-14
 */
@Service
@Slf4j
public class FileUploadLogServiceImpl extends ServiceImpl<FileUploadLogMapper, FileUploadLog> implements FileUploadLogService {

    @Autowired
    private FileUploadLogMapper fileUploadLogMapper;

    @Override
    public void savaLog(Integer businessCode, String moduleName, String fileName, Integer uploadStatus, String uploadDetail) {
        try {
            Object[] parms = {businessCode,moduleName, fileName, uploadStatus, uploadDetail};
            for (Object parm : parms) {
                if (parm == null || Constant.EMPTY_STR.equals(parm)) {
                    log.error("file_upload_log 参数错误: param" + parm);
                }
            }
            FileUploadLog fileUploadLog = new FileUploadLog();
            //serialNumber流水号，业务编号加上当前时间
            String uploadTime = SimpleDateFormatCache.getPushDate().format(new Date());
            StringBuilder serialNumber = new StringBuilder();
            if(businessCode != null){
                serialNumber.append(businessCode.toString()).append(uploadTime);
                fileUploadLog.setSerialNumber(serialNumber.toString());
            }
            fileUploadLog.setModuleName(moduleName);
            fileUploadLog.setFileName(fileName);
            fileUploadLog.setUploadStatus(uploadStatus);
            fileUploadLog.setUploadDetail(uploadDetail);
            // 2023/06/13 日志容量太大，取消保存
            //fileUploadLogMapper.insert(fileUploadLog);
        }catch (Exception e){
            log.error("save log error: {}",e.getMessage());
        }
    }

    @Override
    public IPage<FileUploadLog> selectPage(LogPageSearchBean logPageSearchBean) {
        Page<FileUploadLog> page = new Page<>(logPageSearchBean.getPage(), logPageSearchBean.getSize());
        LambdaQueryWrapper<FileUploadLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(logPageSearchBean.getModuleName()), FileUploadLog::getModuleName, logPageSearchBean.getModuleName());
        wrapper.like(StringUtils.isNotBlank(logPageSearchBean.getFileName()), FileUploadLog::getFileName, logPageSearchBean.getFileName());
        wrapper.eq(StringUtils.isNotBlank(logPageSearchBean.getSerialNumber()), FileUploadLog::getSerialNumber, logPageSearchBean.getSerialNumber());
        wrapper.eq(logPageSearchBean.getUploadStatus() != null, FileUploadLog::getUploadStatus, logPageSearchBean.getUploadStatus());
        wrapper.eq(FileUploadLog::getDeleteFlag, "0");
        wrapper.eq(FileUploadLog::getTenentId, Context.getCurrentTenantId());
        wrapper.likeRight(FileUploadLog::getOrgCode, Context.getCurrentOrgCode());
        wrapper.orderByDesc(FileUploadLog::getId);
        return super.page(page, wrapper);
    }
}
